<template>
  <div class="content">
    <div class="title">2024年粤教社年会抽奖</div>
    <div style="height: 350px;overflow-y:auto;">
      <img :class="[currentNum.includes(index) ? 'my-img-big': '','my-img']" v-for="(item,index) in imageList" :key="index" :src="item" />
    </div>


    <div style="margin-top: 50px;height: 100px;width: 100%;text-align: center">
      <h2 style="color: #f6e397">获奖者</h2>
      <div
          v-for="(item,index) in awardNum"
          :key="index"
          class="image-award"
          style="">

        <transition>
          <img class="animate__animated animate__bounce" style="width: 100px;height: 100px" :src="imageList[currentNum[item-1]]">
        </transition>
      </div>
    </div>
    <div style="">
      <button class="btn-award begin-award" @click="begin">开始</button>
      <button class="btn-award end-award" @click="stop">结束</button>
    </div>
    <div>
      <select class="select-award" @change="selectAwardType">
        <option value="0">请选择奖项</option>
        <option value="10">三等奖(10人)</option>
        <option value="6">二等奖(6人)</option>
        <option value="3">一等奖(3人)</option>
      </select>
    </div>

    <div class="music-img-div" @click="playMusic">
      <img class="music-img music-play" v-if="bgmStatus" src="https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/10/music.png">
      <img class="music-img" v-else src="https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/10/music_off.png">
    </div>

    <div style="margin-top: 100px;color: #f6e397">©广东教育出版社 出版融合中心技术支持</div>

    <el-dialog
        title="获奖名单"
        :visible.sync="dialogVisible"
        width="50%"
        center>
      <span style="color: #f6e397;font-weight: bolder;font-size: 28px;">
        <div v-for="(item,index) in awardNames" :key="index" style="width: 45%;float: left;height: 50px;line-height: 50px;">
          {{item.departName}}：{{item.name}}
        </div>

        <div style="margin: 0 auto;width: 500px;text-align: center;margin-top: 300px;font-size: 16px;font-weight: normal">恭喜以上员工!</div>

      </span>
      <span slot="footer" class="dialog-footer">
        <el-button style="width: 90%;font-size: 20px;" type="warning"  @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>




  </div>
</template>
<script>
export default {
  name: 'Test',
  data(){
    return{
      msg: '测试',
      imageList: [],
      currentNum: [],
      stopStatus: false,
      timer: null,
      awardNum: 0, // 获奖人数，根据此参数生成currentNum的数量
      dialogVisible: false,
      musicImage: [
          'https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/10/music.png',
          'https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/10/music_off.png'
      ],
      bgmStatus: true,
      audio: new Audio('https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/10/bgm.mp3'),
      zsAudio: new Audio('https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/10/zs.mp3'),
      awardNames: [
        {
          departName: '国家课程事业部',
          name: '张三'
        },
        {
          departName: '地方课程事业部',
          name: '李四'
        },
        {
          departName: '教辅事业部',
          name: '王五'
        },
        {
          departName: '出版融合中心',
          name: '赵六'
        },
        {
          departName: '社务中心',
          name: '张三'
        },
        {
          departName: '营销中心',
          name: '张三'
        },{
          departName: '营销中心',
          name: '张三'
        },{
          departName: '营销中心',
          name: '张三'
        },{
          departName: '营销中心',
          name: '张三'
        },{
          departName: '营销中心',
          name: '张三'
        },
      ],

    }
  },
  mounted() {
    this.playMusic();
    // 加载测试数据
    for (let i = 1; i < 31; i++) {
      let k = Math.ceil(Math.random()*30);
      let path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
      k = Math.ceil(Math.random()*30);
      path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
      k = Math.ceil(Math.random()*30);
      path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
      k = Math.ceil(Math.random()*30);
      path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
      k = Math.ceil(Math.random()*30);
      path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
      k = Math.ceil(Math.random()*30);
      path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
      k = Math.ceil(Math.random()*30);
      path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
      k = Math.ceil(Math.random()*30);
      path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
      k = Math.ceil(Math.random()*30);
      path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
      k = Math.ceil(Math.random()*30);
      path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
      k = Math.ceil(Math.random()*30);
      path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
      k = Math.ceil(Math.random()*30);
      path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
      k = Math.ceil(Math.random()*30);
      path = "https://gjs-resourse.oss-cn-shenzhen.aliyuncs.com/resources/2024/05/img/" + k + ".jpg"
      this.imageList.push(path)
    }
  },
  methods: {
    playMusic(){
      // 音乐播放控制
      this.bgmStatus = !this.bgmStatus
      // 设置循环播放
      this.audio.loop = true

      if (this.bgmStatus){
        this.audio.play()
      }else{
        this.audio.pause()
      }
    },
    begin(){
      if (this.awardNum === 0){

        alert("请选择奖项！");
        return;
      }
      // 开始抽奖
      this.stopStatus = false
      clearInterval(this.timer)



      this.timer = setInterval(() => {
        console.log("开始...")
        this.currentNum = []; // 每次都需要清空
        while (this.currentNum.length !== this.awardNum){
          // 生成随机数
          let randomNum = Math.ceil(Math.random() * this.imageList.length)
          console.log("本次生成的随机数",randomNum)
          // 获奖人不能相同
          if (!this.currentNum.includes(randomNum)){
            this.currentNum.push(Math.ceil(Math.random() * this.imageList.length))
          }
        }
        if (this.stopStatus){
          clearInterval(this.timer)
        }

      }, 200)
    },
    stop(){
      this.stopStatus = true
      this.dialogVisible = true
      this.bgmStatus = false
      this.zsAudio.play();
      this.audio.pause();

    },
    selectAwardType(event){
      this.awardNum = parseInt(event.target.value);
      this.currentNum = []
      console.log(this.awardNum)

    },
  }
}

</script>

<style scoped>
.title{
  height: 100px;
  line-height: 100px;
  color: #ffe0a1;
  font-weight: bolder;
  font-size: 42px;
  cursor: pointer;
}
.content{
  width: 99%;
  text-align: center;
  //margin-top: 50px;
}
.my-img{
  cursor: pointer;
  height: 40px;
  width: 40px;
  border-radius: 5px;
  border: 2px solid #ffe767;
  margin-left: 5px;
  transition: all 0.2s;
}
.my-img-big{
  transform: scale(2.5);
}
.image-award{
  margin: 0 auto;
  height: 100px;
  width: 100px;
  background: #f6ac12;
  display: inline-block;
  border: 2px solid #ffe767;
  margin-left: 5px;
  border-radius: 5px
}
.btn-award{
  height: 60px;
  width: 60px;
  color: #ff0000;
  font-size: 16px;
  font-weight: bolder;
  margin-left: 50px;
  border-radius: 50%;
  background: linear-gradient(45deg, #f6ac12, #f6e397);
  border: 2px solid #f6ac12;
  cursor: pointer;
  position: absolute;
  top: 2%
}
.begin-award{
  left: 50px;
}
.end-award{
  left: 120px;
}
.select-award{
  height: 50px;
  width: 150px;
  text-align: center;
  background: #f6e397;
  border:0;
  border-radius: 10px;
  position: absolute;
  top: 4%;
  right: 1%;
  padding: 10px;
}
.music-img-div{
  height: 50px;
  width: 50px;
  background: #FF0000;
  border:2px solid #f6e397;
  border-radius: 50%;
  cursor: pointer
}
.music-img{
  height: 40px;
  width: 40px;
  margin-top: 5px;
  margin-left: 5px
}
.music-play{
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 50px;
  color: red;
  animation: rotate 2s linear infinite;
  -webkit-animation: rotate 2s linear infinite;
}
@keyframes rotate {
  0% {
    transform: rotate(0);
  }

  25% {
    transform: rotate(90deg);
  }

  50% {
    transform: rotate(180deg);
  }

  75% {
    transform: rotate(270deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

@-webkit-keyframes rotate {
  0% {
    transform: rotate(0);
  }

  25% {
    transform: rotate(90deg);
  }

  50% {
    transform: rotate(180deg);
  }

  75% {
    transform: rotate(270deg);
  }

  100% {
    transform: rotate(360deg);
  }
}
/deep/.el-dialog__header{
  background: #FF0000 !important;
  color: #f6e397;
}
/deep/.el-dialog__title{
  color: #f6e397 !important;
  font-size: 26px;
  font-weight: bolder;
}
/deep/.el-dialog__body{
  background: #ff0000 !important;
}
/deep/.el-dialog__footer{
  background: #ff0000 !important;
}
</style>
